<template>
  <el-dialog :title="formData.id?'编辑部门':'新增部门'" :visible="visible" width="50%" @close="closeFn">
    <el-form ref="kunkun" label-width="120px" :model="formData" :rules="formRules">
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="formData.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="formData.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="formData.manager"
          style="width: 80%"
          placeholder="请选择"
          @focus="getEmployeesSimple"
        >
          <el-option
            v-for="item in employeesList"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>

      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="formData.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeFn">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addDepartment, updateDepartment } from '@/api/department'
import { getEmployeesSimple } from '@/api/employees'
export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    currentNode: {
      type: Object,
      required: true
    },
    originDepts: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      formData: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      formRules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '要求1~50个字符哦~', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              let isRepeat = false
              if (this.formData.id) {
                isRepeat = this.originDepts.filter(item => {
                  return item.pid === this.formData.pid && item.id !== this.formData.id
                }).some(item => item.name === value)
              } else {
                isRepeat = this.currentNode.children?.some(
                  (item) => item.name === value
                )
              }
              isRepeat ? callback(new Error('部门名称重复')) : callback()
            },
            trigger: 'blur'
          }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '要求1~50个字符哦~', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              const isRepeat = this.originDepts.some(
                (item) => item.code === value
              )
              isRepeat ? callback(new Error('部门编码重复')) : callback()
            },
            trigger: 'blur'
          }
        ],
        message: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          {
            trigger: 'blur',
            min: 1,
            max: 300,
            message: '部门介绍要求1-50个字符'
          }
        ]
      },
      employeesList: []
    }
  },
  methods: {
    closeFn() {
      this.formData = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
      this.$refs.kunkun.resetFields()
      this.$emit('update:visible', false)
    },
    submit() {
      this.$refs.kunkun.validate(async(isOk) => {
        if (isOk) {
          if (this.formData.id) {
            await updateDepartment(this.formData)
          } else {
            await addDepartment({ ...this.formData, pid: this.currentNode.id })
          }

          this.$emit('updateDeps')
          this.closeFn()
        }
      })
    },

    async getEmployeesSimple() {
      this.employeesList = await getEmployeesSimple()
    }

  }
}
</script>

<style scoped>
.dialog-footer {
  width: 100%;
  display: inline-block;
  text-align: center;
}
</style>
